package py.org.atom.seam.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import py.org.atom.seam.util.Sys;

@MappedSuperclass
public class InsAudit extends Base{

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = Sys.INSERT_DATE_COLUMN, length = 29, nullable=false)	
	private Date insdate;	

	@Column(name = Sys.INSERT_USER_COLUMN, length = Sys.ID_MAXLEN, nullable=false)
	private String insusr;

	public Date getInsdate() {
		return insdate;
	}

	public void setInsdate(Date insdate) {
		this.insdate = insdate;
	}

	public String getInsusr() {
		return insusr;
	}

	public void setInsusr(String insusr) {
		this.insusr = insusr;
	}
	
	@PrePersist
	public void beforePersist(){
		
		String usr = this.getUsr();
		
		if(usr != null){
			this.setInsusr(usr.trim());
			this.setInsdate(new Date());
		}
	}
	
	@Transient
	private String getUsr(){
		if(this.getIdentity() != null 
				&& this.getIdentity().getCredentials() != null
				&& this.getIdentity().getCredentials().getUsername() != null)
			return this.getIdentity().getCredentials().getUsername().trim();
		else
			return null;
	}	
	
}
